package com.alipay.sofa.award.endpoint.impl;

import com.alibaba.common.lang.StringUtil;
import com.alipay.sofa.award.biz.service.facade.ActivityTimeFacade;
import com.alipay.sofa.award.common.dal.dataobject.ActivityTime;
import com.alipay.sofa.award.common.utils.DateFormatUtils;
import com.alipay.sofa.award.endpoint.exception.ActivityTimeErrorCodeDescImpl;
import com.alipay.sofa.award.endpoint.exception.CommonException;
import com.alipay.sofa.award.endpoint.facade.ActivityTimeRestFacade;
import com.alipay.sofa.award.endpoint.response.RestSampleFacadeResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * ActivityTimeRestImpl
 *
 * @author yangguanchao
 * @since 2018/08/27
 */
public class ActivityTimeRestImpl implements ActivityTimeRestFacade {

    private static final Logger logger = LoggerFactory.getLogger(ActivityTimeRestImpl.class);

    @Autowired
    private ActivityTimeFacade  activityTimeFacade;

    @Override
    public RestSampleFacadeResp<Map<String, Object>> activityTime() throws Exception {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("activityTimeList",
            ascendingActivityTimeList(this.activityTimeFacade.queryAllActivityTime()));
        data.put("currentTime", DateFormatUtils.parseDate(new Date()));
        return new RestSampleFacadeResp<>(data);
    }

    @Override
    public RestSampleFacadeResp<Integer> addActivityTime(ActivityTime activityTime)
                                                                                   throws CommonException {
        return new RestSampleFacadeResp<Integer>(
            this.activityTimeFacade.insertActivityTime(activityTime));
    }

    @Override
    public RestSampleFacadeResp<Integer> deleteActivityTime(String activityTime)
                                                                                throws CommonException {
        if (StringUtil.isBlank(activityTime)) {
            throw new CommonException(ActivityTimeErrorCodeDescImpl.ACTIVITY_TIME_ERROR_CODE_DESC);
        }
        return new RestSampleFacadeResp<Integer>(
            this.activityTimeFacade.deleteByUniqueKeyActivityTime(activityTime));
    }

    @Override
    public RestSampleFacadeResp<Integer> updateUser(ActivityTime activityTime)
                                                                              throws CommonException {
        return new RestSampleFacadeResp<Integer>(
            this.activityTimeFacade.updateActivityTime(activityTime));
    }

    public static List<ActivityTime> ascendingActivityTimeList(List<ActivityTime> activityTimeList) {
        if (activityTimeList == null || activityTimeList.size() <= 0) {
            return activityTimeList;
        }
        activityTimeList.sort(new Comparator<ActivityTime>() {
            @Override
            public int compare(ActivityTime o1, ActivityTime o2) {
                Date o1Date = null;
                Date o2Date = null;
                try {
                    o1Date = DateFormatUtils.parseString(o1.getActivityTime());
                    o2Date = DateFormatUtils.parseString(o2.getActivityTime());
                } catch (Exception e) {
                    logger.error("ascendingActivityTimeList error", e);
                }
                if (o1Date == null || o2Date == null) {
                    //exception
                    return 0;
                }
                long o1Time = o1Date.getTime();
                long o2Time = o2Date.getTime();
                return Long.compare(o1Time, o2Time);
            }
        });
        return activityTimeList;
    }
}
